package com.aceleeyy.sevenstar.utils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import com.google.common.collect.Maps;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;

/**
 * @auther: Ace Lee
 * @date: 2019/10/28 22:36
 */
public class ItextpdfUtil {
    private static String PDFPATH = "D:\\7Star\\pdf\\2019\\7月\\七星指南7期（定稿）.pdf";
    private static String FILEPATH = "D:\\7Star\\pdf\\2019\\7月\\七星指南7期（定稿）1.doc";
    public static void main(String[] args)  {
        String content=getPdfContent(PDFPATH);
        System.out.println(content);

        System.out.println(PDFPATH.lastIndexOf("."));

        toFile(PDFPATH,FILEPATH);
    }

    /**
     * 获取pdf的内容和页数
     * @param pdfPath
     * @return
     */
    public static Map<String,Object> getPdfContentAndPages(String pdfPath) {
        PdfReader reader = null;
        StringBuffer buff = new StringBuffer();
        Map<String,Object> result = Maps.newHashMap();
        try {
            reader = new PdfReader(pdfPath);
            PdfReaderContentParser parser = new PdfReaderContentParser(reader);
            int num = reader.getNumberOfPages();// 获得页数
            result.put("count",num);
            TextExtractionStrategy strategy;
            for (int i = 1; i <= num; i++) {
                strategy = parser.processContent(i,
                        new SimpleTextExtractionStrategy());
                buff.append(strategy.getResultantText());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        result.put("content",buff.toString());
        return result;
    }

    /**
     * 获取pdf的内容
     * @param pdfPath
     * @return
     */
    public static String getPdfContent(String pdfPath) {
        PdfReader reader = null;
        StringBuffer buff = new StringBuffer();
        try {
            reader = new PdfReader(pdfPath);
            PdfReaderContentParser parser = new PdfReaderContentParser(reader);
            int num = reader.getNumberOfPages();// 获得页数
            TextExtractionStrategy strategy;
            for (int i = 1; i <= num; i++) {
                strategy = parser.processContent(i,
                        new SimpleTextExtractionStrategy());
                buff.append(strategy.getResultantText());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buff.toString();
    }
    /**
     * 将对应的pdf文件读到指定的文件中
     * @param pdfPath
     * @param filePath
     */
    public static void toFile(String pdfPath, String filePath) {
        PrintWriter writer = null;
        PdfReader reader = null;
        try {
            writer = new PrintWriter(new FileOutputStream(filePath));
            reader = new PdfReader(pdfPath);
            int num = reader.getNumberOfPages();// 获得页数
            System.out.println("Total Page: " + num);
            StringBuffer content = new StringBuffer(""); // 存放读取出的文档内容
            for (int i = 1; i <= num; i++) {
                // 读取第i页的文档内容
                content.append(PdfTextExtractor.getTextFromPage(reader, i));
            }
            writer.write(content.toString());// 写入文件内容
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
